
package org.dyndns.cooland.updater;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dyndns.cooland.updater.db.dao.AddonDAO;
import org.dyndns.cooland.updater.db.dao.CategoryDAO;
import org.dyndns.cooland.updater.db.model.Addon;
import org.dyndns.cooland.updater.db.model.Author;
import org.dyndns.cooland.updater.db.model.Category;
import org.xml.sax.InputSource;

/*
 * parser of catalog xml
 */

public class CatalogParser {
	
	// TODO catalog version check
	
	String	catalog_version	= "1.0";
	
	public CatalogParser () {

	}
	
	public void parse(File catalog, final int site_id) throws IOException,
			DocumentException {

		SAXReader reader = new SAXReader();
		reader.addHandler("/catalog/addon", new ElementHandler() {
			
			public void onStart(ElementPath path) {

			}
			
			public void onEnd(ElementPath path) {

				Element addon = path.getCurrent();
				// Element addonSet = addon.getParent();
				Document doc = addon.getDocument();
				Element root = doc.getRootElement();
				for (int i = 0, size = root.nodeCount(); i < size; i++){
					Node node = root.node(i);
					if (root.node(i) instanceof Element){
						// addon element
						Element real_addon = (Element) node;
						Addon bean = new Addon();
						
						bean.setName(real_addon.selectSingleNode("name")
								.getText());
						bean.setVersion(real_addon.selectSingleNode("version")
								.getText());
						bean.setDescription(real_addon.selectSingleNode(
								"description").getText());
						
						Element download = (Element) real_addon
								.selectSingleNode("download");
						bean.setSource(download.selectSingleNode("source")
								.getText());
						bean.setChecksum(download.selectSingleNode("checksum")
								.getText());
						bean.setSize(Integer.parseInt(download
								.selectSingleNode("size").getText()));
						
						String category_name = real_addon.selectSingleNode(
								"category").getText();
						CategoryDAO cdao = new CategoryDAO();
						Category category = cdao.selectByName(category_name);
						if (category != null){
							bean.setCategory_id(category.getId());
						}
						else{
							category = new Category();
							category.setName(category_name);
							cdao.insert(category);
							bean.setCategory_id(category.getId());
						}
						
						bean.setSite_id(site_id);
						
						Element authors = (Element) real_addon
								.selectSingleNode("authors");
						List<Element> author = (List<Element>) authors
								.selectNodes("author");
						Iterator ir = author.iterator();
						List<Author> _authors = new ArrayList<Author>();
						while (ir.hasNext()){
							Element a = (Element) ir.next();
							String nick = a.attributeValue("nickname");
							String email = a.attributeValue("email");
							String web = a.attributeValue("web");
							Author auth = new Author();
							auth.setEmail(email);
							auth.setNickname(nick);
							auth.setWeb(web);
							_authors.add(auth);
						}
						
						// bean.setAuthors(_authors);
						
						AddonDAO dao = new AddonDAO();
						// int addonId = dao.add(bean);
						dao.insert(bean);
					}
				}
				addon.detach();
			}
		});
		
		// Fix UTF-8 error
		InputStream is = new FileInputStream(catalog);
		Reader freader = new InputStreamReader(is, "UTF-8");
		InputSource ins = new InputSource(freader);
		ins.setEncoding("UTF-8");
		reader.read(ins);
		
	}
	
}
